﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Server.pages
{
    public partial class FrmSinger : UITitlePage
    {
        public FrmSinger()
        {
            InitializeComponent();
        }

        private void FrmSinger_Load(object sender, EventArgs e)
        {

            LoadSingerData();
            ToolStripMenuItem item1 = new ToolStripMenuItem("编辑") { };
            ToolStripMenuItem item2 = new ToolStripMenuItem("删除") { };
            item1.Click += Item1_Click;
            item2.Click += Item2_Click;
            uiContextMenuStrip1.Items.Add(item1);
            uiContextMenuStrip1.Items.Add(item2);
        }

        public void LoadSingerData() {
            // 家里数据库
            string str = "Data Source =.;Initial Catalog=MyKTV;User ID=sa;Password=123@qwe";
            // 学校数据库
            //string str = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=123456@qwe";
            string sql = "select * from sys_singer";
            SqlConnection connection = new SqlConnection(str);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
            this.dsSinger = new DataSet();
            adapter.Fill(dsSinger, "sys_singer");
            this.uiDataGridView1.DataSource = dsSinger.Tables["sys_singer"];
            this.uiPagination1.DataSource = dsSinger.Tables["sys_singer"];
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            uiDataGridView1.DataSource = pagingSource;
        }

        private void Item1_Click(object sender, EventArgs e)
        {
            int a = this.uiDataGridView1.SelectedIndex;
            int singer_id = Convert.ToInt32(this.uiDataGridView1.Rows[a].Cells[0].Value);
            FrmSingerEdit frmSingerEdit = new FrmSingerEdit();
            frmSingerEdit.Singer_id = singer_id;
            frmSingerEdit.ShowDialog();
        }

        private void Item2_Click(object sender, EventArgs e)
        {
            if (this.ShowAskDialog("确认删除这条数据吗？"))
            {
                int a = this.uiDataGridView1.SelectedIndex;
                int singer_id = Convert.ToInt32(this.uiDataGridView1.Rows[a].Cells[0].Value);
                // 家里数据库
                string str = "Data Source =.;Initial Catalog=MyKTV;User ID=sa;Password=123@qwe";
                SqlConnection connection = new SqlConnection(str);
                connection.Open();
                string sql = "delete from sys_singer where id = " + singer_id;
                SqlCommand command = new SqlCommand(sql,connection);
                int number = command.ExecuteNonQuery();
                if (number > 0)
                {
                    this.ShowSuccessTip("删除成功！");
                    LoadSingerData();
                }
                else
                {
                    this.ShowErrorDialog("删除失败！");
                }

            }
            else
            {

                this.ShowErrorTip("您点击了取消按钮");
            }
        }
    }
}
